home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1327 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.5 KB

  1. Path: news.compuserve.com!newsmaster
  2. From: 73700.776@compuserve.com (Walter C. Riley)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Syntax for looping through enumerated types
  5. Date: Wed, 10 Jan 1996 13:34:41 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4d0fe1$947@dub-news-svc-5.compuserve.com>
  8. References: <4cs70o$8ns@ornews.intel.com>
  9. NNTP-Posting-Host: dd37-136.compuserve.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. thurman_b_miller@ccm2.hf.intel.com (Thurman Miller) wrote:
  13.  
  14. >Suppose I have:
  15.  
  16. >enum daysoftheweek {MONDAY, TUESDAY, WEDNESDAY,THURSDAY,FRIDAY} ;
  17.  
  18. >How can I loop through this using a for statement?
  19.  
  20. >(the following doesn't work)
  21.  
  22. >daysoftheweek nIndex;
  23. >for (nIndex=MONDAY;nIndex<=FRIDAY;nIndex++)
  24. >{
  25.  
  26. >    switch (nIndex):
  27. >    {
  28. >        case  MONDAY:
  29. >            doMonday();
  30. >    }
  31.  
  32. >}
  33.  
  34. >I'd like to get away from specifying the beginning & end (ie MONDAY &
  35. >TUESDAY) if possible because my enumerated list will constanty grow
  36. >and I'd like to only change the definition of the enumerated list and
  37. >the for statement would continue to work.
  38.  
  39. >Note: This is just a trivial example, not what I'm actually wanting to
  40. >do.
  41.  
  42. >TIA
  43.  
  44. >Thurman Miller
  45.  
  46. Your code should work - you do have a colon at the end of the switch
  47. line that does not belong, but I'm guessing that this is a typo. (and
  48. you do have a main(), don't you? <g>)
  49.  
  50. If the enum will change often, this is not a good approach, however.
  51. You likely will be better off using some container class, eg a list,
  52. array, or dictionary class that best suits what you want to do..
  53.  
  54. Good luck,
  55. Walt
  56.  
  57.  
  58.